<template>
  <div>
    <a-select
        :loading="dataLoading"
        :style="{minWidth: '240px'}"
        showSearch
        :filter-option="filterOption"
        v-model="organization_id"
        @change="handleChange"
    >
      <a-select-option v-for="item in departments" :key="item.id">
        {{item.name}}
      </a-select-option>
    </a-select>
  </div>
</template>

<script>
import {get_organization_list} from "@/services/manage";
import storage from "store";
import {ORGANIZATION_ID, USER_ORGANIZATION_ID} from "@/store/mutation-types";
import {uniqueReduce} from "@/utils/util";

export default {
  name: "HeaderCompany",
  components: {},
  data () {
    return {
      dataLoading: false, // 下拉列表的加载状态
      departments: [], //所有部门原始数据
      organization_id: undefined,
      departments01: [],
    }
  },
  methods: {
    // 输入框筛选
    filterOption(input, option){
      return (
          option.componentOptions.children[0].text.toUpperCase().indexOf(input.toUpperCase()) >= 0
      );
    },
    handleChange() {
      storage.remove('Organization-Id')
      storage.set('Organization-Id', this.organization_id, 15 * 60 * 60)
      const obj = this.departments.find(el => el.id === this.organization_id)
      const dept_id = obj?obj.dept_id:undefined
      storage.remove('Dept-Id')
      storage.set('Dept-Id', dept_id, 15 * 60 * 60)
      this.$refreshPage(this.$route.path)
    },
    // 获取所有部门信息
    getDepartment(){
      const params = {}
      return new Promise(resolve => {
        get_organization_list(params).then(async ({data: res}) => {
          if (res.code === '1000') {
            this.departments01 = []
            let role_department = []
            this.departments01 = res.data
            if (this.departments01.find(el => el.id === this.organization_id)) {
              storage.remove('Organization-Id')
              storage.set('Organization-Id', this.departments01.find(el => el.id === this.organization_id).organization_id, 15 * 60 * 60)
              this.organization_id = storage.get(ORGANIZATION_ID)
            }
            const roleDept = storage.get('role_relation')
            if (roleDept && roleDept.length > 0) {
              roleDept.forEach(el => {
                if (this.departments01.find(item => item.id === el.organization_id)) {
                  const param = this.departments01.find(item => item.id === el.organization_id)
                  role_department.push({
                    extension1: el.extension1,
                    id: param.id,
                    name: param.name,
                    code: param.code,
                    dept_id: el.organization_id
                  })
                }
              })
              const dpList = uniqueReduce(role_department, ['extension1'])
              dpList.forEach(el => {
                if (this.departments01.find(item => item.name === el.extension1)) {
                  const param = this.departments01.find(item => item.name === el.extension1)
                  this.departments.push({
                    id: param.id,
                    // code: param.code,
                    name: param.name,
                    dept_id: el.dept_id
                  })
                }
              })
            }
            resolve()
          } else if (res.code === '1002') {
            this.departments = []
            resolve()
          }
        })
            .catch(err => {
              console.log(err)
              resolve()
            })
      })
    },
  },
  mounted() {
    this.getDepartment()
  },
  created() {
    this.organization_id = storage.get(ORGANIZATION_ID)?storage.get(ORGANIZATION_ID):storage.get(USER_ORGANIZATION_ID)
  }
}
</script>

<style scoped>
/deep/ .ant-select-selection{
  background-color: #fff;
}
</style>